import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    currentDate: new Date().getTime(),
    appointment: '',
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    houseId: '',
    houseInfo: '',
    repairItemId: '',
    repairItemName: '',
    mobile: '',
    description: '',
    repairItem: [],
    attachment: [],
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修的房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }],
  },
  onLoad({ id }) {
    console.log(id)
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      this.updataRepair(id)
    }
  },
  // 编辑时获取回显的报修信息
  async updataRepair(id) {
    const { code, data } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast('查询详情错误')
    console.log(data)
    this.setData(data)
  },
  //查询我能报修的房屋
  async getHouseList() {
    const { data: houseList, code } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast('查询可报修房屋失败')
    // console.log(houseList)
    this.setData({
      houseList,
    })
  },
  // 点击报修房屋展示弹层
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 选择报修具体项时触发
  selectHouseInfo(ev) {
    // console.log(ev.detail)
    this.setData({
      houseId: ev.detail.id,
      houseInfo: ev.detail.name,
    })
  },
  // 关闭报修弹层
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 查询维修项目
  async getRepairItem() {
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast('查询维修项目失败')
    // console.log(repairItem)
    this.setData({
      repairItem,
    })
  },
  // 点击弹出选择报修项目的弹层
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  // 选择维修明星们具体项触发
  selectRepairInfo(ev) {
    console.log(ev.detail)
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name,
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // 展示选择日期弹层展示
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  // 选择日期点击确定触发
  selectDateInfo(ev) {
    // 获得一个选择日期的时间戳
    const appointment = wx.utils.FormaTime(ev.detail)
    // console.log(time)
    this.setData({ appointment })
    this.closeDateLayer()
  },
  // 关闭日期弹层
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 上传图片
  afterRead(ev) {
    // console.log(ev.detail.file.url)
    wx.uploadFile({
      filePath: ev.detail.file.url,
      name: 'file',
      url: wx.http.baseURL + '/upload',
      header: { Authorization: 'Bearer ' + getApp().token },
      success: ({ data }) => {
        const res = JSON.parse(data)
        if (res.code !== 10000) return wx.utils.toast('上传图片失败')
        this.data.attachment.push(res.data)
        this.setData({ attachment: this.data.attachment })
      },
    })
  },
  // 删除图片
  delafterRead(ev) {
    // console.log(ev.detail.file.id)
    this.setData({
      attachment: this.data.attachment.filter((item) => item.id !== ev.detail.file.id),
    })
  },
  // 提交
  async goList() {
    if (!this.validate()) return
    const { houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const { code } = await wx.http.post('/repair', {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    if (code !== 10000) return wx.utils.toast('提交失败')
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
